package leo.manager
{
	import flash.display.BitmapData;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	
	import leo.data.GameConfig;
	import leo.data.GameConst;
	import leo.data.GameModel;
	import leo.data.MonsterVO;
	import leo.game.display.BaseMonster;
	import leo.game.stage.Game;
	import leo.game.ui.UIMap;

	/**
	 * 怪物管理器 
	 * @author leo
	 * 
	 */
	public class MonsterManager
	{
		private static var _instance :MonsterManager = null;
		/**
		 * 当前出怪索引 
		 */		
		private var index :int = 0;
		/**
		 * 时间器 
		 */		
		private var timer :Timer;
		/**
		 * 刷新时间 
		 */		
		private var refreshTimer :int;
		public function MonsterManager()
		{
			this.timer = new Timer(Game.instance.refreshInterval);
			this.timer.addEventListener(TimerEvent.TIMER,timerHandler);
		}
		
		public function createMonster(battleNumber :int):void
		{
			var monsterName :String = GameConfig.monster_list[battleNumber];
			var imgDict :Dictionary = GameModel.instance.imgDict;
			var monsterList :Vector.<BaseMonster> = GameModel.instance.monsterList;
			if(imgDict[monsterName])
			{
				var level :int = GameModel.instance.curLevel;
				var startPoint :Point = GameConfig.m_start_point_ary[level];
				var delay :int = 100;
				for(var i :int = 0; i < GameConfig.level_monster_num ; i++)
				{
					var monsterVO :MonsterVO = VOManager.instance.getMonsterVOByName(monsterName);
					monsterVO = VOManager.instance.copyMonsterVO(monsterVO);
					var bmd :BitmapData = imgDict[monsterName] as BitmapData;
					var w :int = GameConst.IMG_TILE_WIDHT;
					var h :int = GameConst.IMG_TILE_HEIGHT;
					monsterVO.bmdDict[monsterVO.dir_up * monsterVO.ratio] = BitmapManager.instance.sparateBitmapData(bmd,w,h,2,0,4);  //上
					monsterVO.bmdDict[monsterVO.dir_down * monsterVO.ratio] = BitmapManager.instance.sparateBitmapData(bmd,w,h,0,0,4); //下
					monsterVO.bmdDict[monsterVO.dir_left * monsterVO.ratio] = BitmapManager.instance.sparateBitmapData(bmd,w,h,1,0,4);  //左
					var tempLeft :Vector.<BitmapData> = BitmapManager.instance.sparateBitmapData(bmd,w,h,1,0,4);
					monsterVO.bmdDict[monsterVO.dir_right * monsterVO.ratio] = BitmapManager.instance.rotationBitmapData(tempLeft); //右
					
					var monster :BaseMonster = new BaseMonster(monsterVO,delay);
					monster.x = startPoint.x * GameConst.TILE_WIDTH;
					monster.y = startPoint.y * GameConst.TILE_HEIGHT;
					delay += 2000;
					monsterList.push(monster);
				}
				GameModel.instance.curMapMonsterNum = monsterList.length;
				this.timer.start();
			}
		}
		
		private function timerHandler(event :TimerEvent):void
		{
			if(this.index == 0)
			{
				add();
			}else
			{
				this.refreshTimer += Game.instance.refreshInterval;
				if(this.refreshTimer >= GameConfig.create_monster_time)
				{
					add();
				}
			}
		}
		/**
		 * 增加怪物到地图上 
		 * 
		 */		
		private function add():void
		{
			var monsterList :Vector.<BaseMonster> = GameModel.instance.monsterList;
			if(monsterList.length > 0)
			{
				if(index > monsterList.length - 1) 
				{
					this.index = 0;
					this.timer.stop();
				}
				if(monsterList[index])
				{
					var monster :BaseMonster = monsterList[index] as BaseMonster;
					UIMap.instance.addItem(monster);
					this.refreshTimer = 0;
					this.index++;
				}
			}
		}
		/**
		 * 重置延迟出怪设置 
		 * 
		 */		
		public function resetMonsterManager():void
		{
			this.timer.reset();
			this.timer.stop();
			this.refreshTimer = 0;
			this.index = 0;
			deleteMonster();
		}
		
		/**
		 * 删除怪物
		 * 
		 */		
		private function deleteMonster():void
		{
			//输了或者赢了再删除怪物，防止延迟加入怪的时候就删掉了 
			var monsterList :Vector.<BaseMonster> = GameModel.instance.monsterList;
			for(var monsterNum :int = monsterList.length - 1 ; monsterNum >= 0; monsterNum--)
			{
				var monster :BaseMonster = monsterList[monsterNum] as BaseMonster;
//				if(!monster.isAlive)
//				{
					UIMap.instance.removeItem(monster);
					monsterList.splice(monsterNum,1);
//				}
			}
		}
		
		
		public static function get instance():MonsterManager
		{
			if(_instance == null)
			{
				_instance =  new MonsterManager();
			}
			return _instance;
		}

	}
}